/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */

package com.ykht.proj.coupon.controller.c;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ykht.proj.common.core.util.R;
import com.ykht.proj.common.security.util.SecurityUtils;
import com.ykht.proj.coupon.entity.CnCare;
import com.ykht.proj.coupon.entity.CnPerson;
import com.ykht.proj.coupon.service.CnCareService;
import com.ykht.proj.coupon.vo.CnCarePageResp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;


/**
 * 收藏表
 *
 * @author proj code generator
 * @date 2020-11-16 17:00:21
 */
@RestController
@AllArgsConstructor
@RequestMapping("c/care")
@Api(value = "care", tags = "c端关注相关")
@Slf4j
public class CareController {

	private final CnCareService cnCareService;

	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/page")
	public R<IPage<CnCarePageResp>> page(Page page, CnCare cnCare) {
		log.info("->分页查询关注：{}", JSONUtil.toJsonStr(cnCare));
		IPage<CnCarePageResp> resp = cnCareService.opPage(page, cnCare);
		return R.ok(resp);
	}


	@ApiOperation(value = "添加关注", notes = "添加关注")
	@PostMapping("add")
	public R add(@RequestBody CnCare cnCare) {
		log.info("->添加关注：{}", JSONUtil.toJsonStr(cnCare));
		Long loginId = SecurityUtils.getPersonId();


		CnPerson cnPerson = new CnPerson();
		cnPerson.setId(loginId);
		cnPerson = cnPerson.selectById();
		//有手机号
		if (0 != cnPerson.getAuthBaseStatus()) return R.failed("未授权微信基础信息");
		CnCare temp = cnCareService.getOne(Wrappers.lambdaQuery(CnCare.class)
				.eq(CnCare::getPerId, loginId).eq(CnCare::getTgtId, cnCare.getTgtId()).eq(CnCare::getType, 0));

		if (temp != null) return R.ok();
		cnCare.setPerId(loginId);
		cnCare.setType(0);
		cnCare.setCareTime(LocalDateTime.now());
		cnCare.setCreateTime(LocalDateTime.now());
		cnCare.setCreateUser(loginId);
		return R.ok(cnCareService.save(cnCare));
	}

	@ApiOperation(value = "移除关注", notes = "移除关注")
	@PostMapping("/remove")
	public R removeById(@RequestBody CnCare cnCare) {
		log.info("->移除关注：{}", JSONUtil.toJsonStr(cnCare));
		Long loginId = SecurityUtils.getPersonId();
		CnPerson cnPerson = new CnPerson();
		cnPerson.setId(loginId);
		cnPerson = cnPerson.selectById();
		//有手机号
		if (0 != cnPerson.getAuthBaseStatus()) return R.failed("未授权微信基础信息");
		cnCareService.remove(Wrappers.lambdaQuery(CnCare.class)
				.eq(CnCare::getTgtId, cnCare.getTgtId())
				.eq(CnCare::getPerId, SecurityUtils.getPersonId()).eq(CnCare::getType, 0));
		return R.ok();
	}
}
